﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jzy.Core.Common;
using FortuneHeavenCommon.DataMap;
using FortuneHeavenCommon.Enum;

namespace FortuneHeavenDAL
{
    public class NewsDAL : BaseDAL
    {
        public List<News> GetNews(int page, int size, NewsType type)
        {
            string sql = @"select * from ( select A.[ID],A.[Title],A.[Content],A.[Time],A.[NewsType],A.[Status],A.SubmitUser,B.RealName as SubmitUserName,
        ROW_NUMBER() OVER(ORDER BY A.ID DESC) ROW  from News A left join AdminUser B 
        ON B.ID= A.SubmitUser WHERE NewsType =  @p0 ) AS FF 
            WHERE FF.ROW BETWEEN  @p1 AND @p2";
            List<News> list = dbOper.Query<News>(sql, (int)type, size * (page - 1)+1, size * page);
            return list;
        }
        public int GetTotalSize(NewsType type)
        {
            string sql = @"select count(id) from News where NewsType =@p0";
            return dbOper.GetScalar<int>(sql, (int)type);
        }
        public List<News> GetTopNews(int size,  NewsType type) {
            string sql = @"select top {0} * from News WHERE NewsType =  @p0 order by Time desc";

            List<News> list = dbOper.Query<News>(string.Format(sql,size), (int)type);
            return list;
        }

        public News GetNewsByTitle(string name)
        {
            string sql = @"select * from News WHERE Title =@p0";

            List<News> list = dbOper.Query<News>(sql, name);
            if (list != null&&list.Count>0)
            {
                return list[0];
            }
            return null;
        }
    }
}
